package com.lucky.tjs.config;

import com.dangdang.ddframe.job.api.ElasticJob;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.JobTypeConfiguration;
import com.dangdang.ddframe.job.config.dataflow.DataflowJobConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import com.lucky.tjs.ElasticJobAutoConfiguration;
import com.lucky.tjs.annotation.ElasticJobConfig;
import com.lucky.tjs.entity.JobConfig;
import com.lucky.tjs.exception.ConfigException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class InitJobSchedulerHandler {

    @Resource
    private CoordinatorRegistryCenter regCenter;

    public void init( SimpleJob job,JobConfig jobConfig){
        if(job == null){
            throw new NullPointerException("job is null");
        }
        LiteJobConfiguration jobConfiguration = getJobConfiguration(jobConfig,job);
        JobScheduler jobScheduler = new SpringJobScheduler(job, regCenter, jobConfiguration);
        jobScheduler.init();
    }

    private LiteJobConfiguration getJobConfiguration(JobConfig config, SimpleJob elasticJob) {
        String jobName = config.getJobName();
        String cron = config.getCron();
        String jobDescription = config.getJobDescription();
        String shardingItemParameters = config.getShardingItemParameters();
        int shardingTotalCount = config.getShardingTotalCount();
        JobCoreConfiguration.Builder builder = JobCoreConfiguration.newBuilder(
                //作业名称
                jobName,
                //CRON 表达式，用于控制作业触发时间
                cron,
                //作业名称
                shardingTotalCount
        );
        JobCoreConfiguration jobCoreConfiguration = builder
                //个性化分片参数
                .shardingItemParameters(shardingItemParameters)
                //作业描述
                .description(jobDescription)
                .build();
        JobTypeConfiguration jobTypeConfiguration = null;
        /*
                简单作业
             */
        jobTypeConfiguration = new SimpleJobConfiguration(
                jobCoreConfiguration, elasticJob.getClass().getCanonicalName()
        );
        return LiteJobConfiguration
                .newBuilder(jobTypeConfiguration)
                .overwrite(true)
                .jobShardingStrategyClass("com.dangdang.ddframe.job.lite.api.strategy.impl.RotateServerByNameJobShardingStrategy")//分片策略
                .build();
    }

    protected LiteJobConfiguration getJobConfiguration(ElasticJobConfig config, ElasticJob elasticJob) {
        String jobName = config.jobName();
        String cron = config.cron();
        String jobDescription = config.jobDescription();
        String shardingItemParameters = config.shardingItemParameters();
        boolean misfire = config.misfire();
        int shardingTotalCount = config.shardingTotalCount();
        boolean failover = config.failover();
        String jobParameter = config.jobParameter();
        boolean streamingProcess = config.streamingProcess();
        JobCoreConfiguration.Builder builder = JobCoreConfiguration.newBuilder(
                //作业名称
                jobName,
                //CRON 表达式，用于控制作业触发时间
                cron,
                //作业名称
                shardingTotalCount
        );
        JobCoreConfiguration jobCoreConfiguration = builder
                //个性化分片参数
                .shardingItemParameters(shardingItemParameters)
                //作业描述
                .description(jobDescription)
                //作业自定义参数
                .jobParameter(jobParameter)
                //是否开启错过任务重执行               默认true
                .misfire(misfire)
                //是否开启是否开启任务执行失效转移      默认false
                .failover(failover)
                .build();
        JobTypeConfiguration jobTypeConfiguration = null;
        if(elasticJob instanceof SimpleJob){
            /*
                简单作业
             */
            jobTypeConfiguration = new SimpleJobConfiguration(
                    jobCoreConfiguration, elasticJob.getClass().getCanonicalName()
            );
        }else {
            /*
                数据流作业
             */
            jobTypeConfiguration = new DataflowJobConfiguration(
                    jobCoreConfiguration, elasticJob.getClass().getCanonicalName(),
                    //是否开启流式处理
                    streamingProcess
            );

        }
        return LiteJobConfiguration
                .newBuilder(jobTypeConfiguration)
                .jobShardingStrategyClass("com.dangdang.ddframe.job.lite.api.strategy.impl.RotateServerByNameJobShardingStrategy")//分片策略
                //本地配置是否可覆盖注册中心配置  默认false
                .overwrite(config.overwrite())
                //最大允许的本机与注册中心的时间误差秒数  默认-1
                .maxTimeDiffSeconds(config.maxTimeDiffSeconds())
                //修复作业服务器不一致状态服务调度间隔分钟
                .reconcileIntervalMinutes(config.reconcileIntervalMinutes())
                //监控作业运行时状态 默认为true
                .monitorExecution(config.monitorExecution())
                //作业是否禁止启动  默认false
                .disabled(config.disabled())
                .build();
    }

}
